$(document).on('ready', function() {
	$("#text-map").find(".text-map-countries").on('mouseenter', function() {
		$(this).find("#x").fadeIn('fast');
		$(this).children().on('click', function() {
			$(this).parent().fadeOut('slow');
			var id = $(this).parent().find("#region_id").text();
			delete_visited(id);
		});
	}).mouseleave(function() {
		$(this).children().fadeOut('fast');
	});
	
	$("#add-location-button").on('click', function() {
		$("#add-location-bar").slideDown();
		$("html, body").animate({ scrollTop: $(document).height() }, "slow");
		return false;
	});
	
	$("#country").on('change', function() {
		var countryID = $("#country option:selected").val();
		$("#region").empty();
		if(countryID == 0) {
			$("#region").attr("disabled", "disabled");
			$(".submit").attr("disabled", "disabled");
			$("#region").append("<option value='0'>Select Region</option>");
		} else {
			$("#region").removeAttr("disabled");
			$(".submit").removeAttr("disabled");
			$(".submit").css("color", "white");
			$(".submit").css("background", "#ffbf0f");
		}
		$.ajax({
			type:'POST',
			url:'get_regions.php',
			data:'country_id='+countryID,
			dataType: 'json',
			beforeSend: function() {
				$("#select-region-loader").fadeIn();
			},
			error: function(request, status, error) {
				 alert(request.responseText);
			},
			success: function(response) {
				for(i=0; i<=response.length-1; i++) {
					$("#region").append("<option value='" + response[i]["id"] + "'>" + response[i]["name"] + "</option>");
				}
			},
			complete: function () {
				$("#select-region-loader").fadeOut();
			}
		});
	});
});

function delete_visited(id) {
	$.ajax({
		type:'POST',
		url:'delete_visited.php',
		data:"region_id="+id,
		error: function(request, status, error) {
			 alert(request.responseText);
		},
		success:function(response){
		}
	});
	for(var i=0; i<existingMarkers.length; i++) {
		if(existingMarkers[i].get("id") == id) {
			//alert(existingMarkers[i]);
			existingMarkers[i].setMap(null);
		}
	}
}

function getMarker(countryID, regionID) {
	$.ajax({
		type:'POST',
		url:'addMarker.php',
		data:"region_id="+regionID+"&country_id="+countryID,
		dataType: 'json',
		error: function(request, status, error) {
			 alert(request.responseText);
		},
		success:function(response){
			if(response == 2) {
				error_report("Country Visited", "You have already marked this country as visited. Please select a new country.");
			} else {
				$("<span class='text-map-countries'>" + response.region + "<span id='x'>&#10006</span><span id='region_id'>" + response.regionID + "</span></span>").prependTo("#text-map").hide().fadeIn();
				$("#text-map").find(".text-map-countries").on('mouseenter', function() {
					$(this).find("#x").fadeIn('fast');
					$(this).children().on('click', function() {
						$(this).parent().fadeOut('slow');
						var id = $(this).parent().find("#region_id").text();
						delete_visited(id);
					});
				}).mouseleave(function() {
					$(this).children().fadeOut('fast');
				});
				addMarker(response.country, response.region, response.latitude, response.longitude, response.regionID);
			}
		}
	});
}

function addMarker(country, region, latitude, longitude, regionID) {
	var newPos = new google.maps.LatLng(latitude, longitude);
	var infowindow = new google.maps.InfoWindow({
		content: " "
	});
	bounds.extend(newPos);
	marker = new google.maps.Marker({
		position: newPos,
		map: map,
		animation: google.maps.Animation.DROP
	});
	marker.set("id", parseInt(regionID));
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.setContent(region+ ", " + country);
		infowindow.open(map, marker);
	});
	map.fitBounds(bounds);
	existingMarkers.push(marker);
}

function add_visited() {
	var chosenCountryID = $("#country").val();
	var chosenRegionID = $("#region").val();
	getMarker(chosenCountryID, chosenRegionID);
	$("#country").val("0");
	$("#region").val("0");
	$("#region").attr("disabled", "disabled");
	$("#add-location-bar").slideUp();
}

function addPlaceMarker(lat, lon) {
	for(var i=0; i<existingMarkers.length; i++) {
		if(existingMarkers[i]) {
			existingMarkers[i].setMap(null);
		}
	}
	var newPos = new google.maps.LatLng(lat, lon);
	var bounds = new google.maps.LatLngBounds();
	bounds.extend(newPos);
	marker = new google.maps.Marker({
		position: newPos,
		map: map,
		animation: google.maps.Animation.DROP
	});
	map.fitBounds(bounds);	
	existingMarkers.push(marker);
}
